#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QEvent>
#include <QMouseEvent>
#include <QApplication>
#include <QDesktopServices>
#include "titlebar.h"
#include "mainwindow.h"
#include "settingpage.h"
QGSettings *TitleBar::m_pGsettingThemeData;
TitleBar::TitleBar(QWidget *parent)
  : QWidget(parent)
{
    // 按钮初始化
    m_pGsettingThemeData = new QGSettings(FITTHEMEWINDOW);  //安装窗口
    funcListButton = new QToolButton(this);
    m_pFuncLabel = new QLabel(this);
    m_pMinimizeButton = new QPushButton(this);
    m_pMaximizeButton = new QPushButton;     //不指定this
    m_pCloseButton = new QPushButton(this);

    funcListButton->setProperty("isWindowButton", 0x1);
    funcListButton->setProperty("useIconHighlightEffect",0x2);
    funcListButton->setAutoRaise(true);

    m_pMinimizeButton->setProperty("isWindowButton", 0x1);
    m_pMinimizeButton->setProperty("useIconHighlightEffect",0x2);
    m_pMinimizeButton->setFlat(true);

    m_maxButtonPressed = false;
    m_pMaximizeButton->setProperty("isWindowButton", 0x1);
    m_pMaximizeButton->setProperty("useIconHighlightEffect",0x2);
    m_pMaximizeButton->setFlat(true);


    m_pCloseButton->setProperty("isWindowButton", 0x2);
    m_pCloseButton->setProperty("useIconHighlightEffect",0x8);
    m_pCloseButton->setFlat(true);

    // 设置空间大小
    //  funcListButton->setFixedSize(30,30);
    m_pFuncLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_pMaximizeButton->setFixedSize(30,30);
    m_pMinimizeButton->setFixedSize(30,30);
    m_pCloseButton->setFixedSize(30,30);
    funcListButton->setFixedSize(30,30);

    //icon = new QLabel;//图标
    icon = new QPushButton(this);
    //icon->setPixmap(QPixmap::fromImage(QImage(":/image/camera-app@2x.png")));
    icon->setIcon(QIcon::fromTheme("kylin-camera"));
    icon->setFixedSize(24,24);
    icon->setIconSize(QSize(24,24));

#ifndef __V10__
    funcListButton->setIcon(QIcon::fromTheme("open-menu-symbolic"));
    m_pMinimizeButton->setIcon(QIcon::fromTheme("window-minimize-symbolic"));
    //m_pCloseButton->setIcon(QIcon::fromTheme("window-close"));
    m_pCloseButton->setIcon(QIcon::fromTheme("window-close-symbolic")); //lhl debug 更改title关闭图标
    m_pMaximizeButton->setIcon(QIcon::fromTheme("window-maximize-symbolic"));
#else
    funcListButton->setIcon(QIcon(":/image/funclist.png"));
    m_pMinimizeButton->setIcon(QIcon(":/image/min.png"));
    m_pCloseButton->setIcon(QIcon(":/image/close.png"));
    m_pMaximizeButton->setIcon(QIcon(":/image/max.png"));
#endif


    m_pCloseButton->setProperty("isWindowButton",0x2);
    m_pCloseButton->setProperty("useIconHighlightEffect",0x8);
//    m_pCloseButton->setAutoRaise(true);
//    funcListButton->setAcceptDrops();

    funcListButton->setProperty("isWindowButton", 0x1);
    funcListButton->setProperty("useIconHighlightEffect", 0x2);
    funcListButton->setPopupMode(QToolButton::InstantPopup);
    funcListButton->setAutoRaise(true);

//    funcListButton->setFocusPolicy(Qt::NoFocus);

    m_pMinimizeButton->setProperty("isWindowButton", 0x1);
    m_pMinimizeButton->setProperty("useIconHighlightEffect", 0x2);
    m_pMinimizeButton->setFlat(true);
    m_pMinimizeButton->setChecked(false);
    m_pMinimizeButton->setFocusPolicy(Qt::NoFocus);

    m_pCloseButton->setProperty("isWindowButton", 0x2);
    m_pCloseButton->setProperty("useIconHighlightEffect", 0x8);
    m_pCloseButton->setFlat(true);
    m_pCloseButton->setFocusPolicy(Qt::NoFocus);

    m_pMaximizeButton->setProperty("isWindowButton", 0x1);
    m_pMaximizeButton->setProperty("useIconHighlightEffect", 0x2);
    m_pMaximizeButton->setFlat(true);
    m_pMaximizeButton->setFocusPolicy(Qt::NoFocus);

    // 设置对象名
    m_pFuncLabel->setObjectName("whiteLabel");
    funcListButton->setObjectName("funcListButton");
    m_pMinimizeButton->setObjectName("minimizeButton");
    m_pMaximizeButton->setObjectName("maximizeButton");
    m_pCloseButton->setObjectName("closeButton");
    // 麒麟摄像头显示
    m_pFuncLabel->setText(tr("kylin-camera"));

    QString labStyle = \
        "font-size: 14px;"
        "line-height: 32px;"
        "opacity: 1;";

    m_pFuncLabel->setStyleSheet(labStyle);
#ifdef __V10__
    m_pFuncLabel->setStyleSheet("color:#A6A6A6;font-size:14px;font-family:SourceHanSansCN-Regular;");
    QString btnStyle = \
                     "QPushButton{border-radius:4px;}"
                     "QPushButton:hover{background-color:rgba(255,255,255,0.1);}"
                     "QPushButton:pressed{background-color:rgba(255,255,255,0.15);}";
    funcListButton->setStyleSheet(btnStyle);
    m_pMinimizeButton->setStyleSheet(btnStyle);
    m_pMaximizeButton->setStyleSheet(btnStyle);
    m_pCloseButton->setStyleSheet("QPushButton{border-radius:4px;}QPushButton:hover{background-color:#F86457;}");
#endif
    //设置背景色白色
    nowThemeStyle = m_pGsettingThemeData->get("styleName").toString(); //lhl debug
    this->setGeometry(0,0,300,100);
    QPalette pal(this->palette());
    //深色模式下
    if (nowThemeStyle == "ukui-dark" || nowThemeStyle == "ukui-black"){
        //设置背景色黑色
        pal.setColor(QPalette::Background,Qt::black);
        icon->setStyleSheet("background-color:black");
    }
    else{
        //设置背景色白色
        pal.setColor(QPalette::Background,Qt::white);
        icon->setStyleSheet("background-color:white");
    }
    this->setAutoFillBackground(true);
    this->setPalette(pal);
    this->show();

    // 设置按钮布局
    QHBoxLayout *pLayout = new QHBoxLayout(this);
    pLayout->addSpacing(8);
    pLayout->addWidget(icon);
    pLayout->addWidget(m_pFuncLabel);
    pLayout->addStretch(-4);
    pLayout->addWidget(funcListButton);
    pLayout->addSpacing(-4);
    pLayout->addWidget(m_pMinimizeButton);
    pLayout->addSpacing(-4);
    pLayout->addWidget(m_pMaximizeButton);
    pLayout->addSpacing(-4);
    pLayout->addWidget(m_pCloseButton);
    pLayout->addSpacing(4);
//    pLayout->setMargin(0);
    pLayout->setContentsMargins(0, 0, 0, 0);
    this->setFixedHeight(38);
    this->setLayout(pLayout);
    this->setObjectName("title");
//    this->setStyleSheet("TitleBar#title{background-color:#000000}");
    //  this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    // 设置信号和槽函数
    connect(m_pMinimizeButton,&QPushButton::clicked,this,&TitleBar::onClicked);
    connect(m_pCloseButton,&QPushButton::clicked,this,&TitleBar::onClicked);
    connect(m_pMaximizeButton,&QPushButton::clicked,this,&TitleBar::onClicked);
    connect(funcListButton,&QToolButton::clicked,this,&TitleBar::onClicked);
    connect(m_pGsettingThemeData,&QGSettings::changed,this,&TitleBar::dealSystemGsettingChange); //lhl debug
}


TitleBar::~TitleBar()
{

}
//void TitleBar::setFuncLabel(QString label)
//{
//    this->m_pFuncLabel->setText(label);
//}

// 双击标题栏进行界面的最大化/还原
//void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
//{
//    Q_UNUSED(event);

//    emit m_pMaximizeButton->clicked();
//}


//按钮功能设置，和槽链接
void TitleBar::onClicked()
{
    QPushButton *pButton = qobject_cast<QPushButton *>(sender());
    QWidget *pWindow = this->window();
    if (pWindow->isTopLevel())
    {
        if (pButton == m_pMinimizeButton)
        {
            pWindow->showMinimized();
        }
        else if (pButton == m_pTopButton)
        {
        }
        else if(pButton == m_pMaximizeButton){
            //窗口最大化
            if(!m_maxButtonPressed){
                pWindow->showMaximized();
            #ifndef __V10__
                m_pMaximizeButton->setIcon(QIcon::fromTheme("window-restore-symbolic"));
            #else
                m_pMaximizeButton->setIcon(QIcon(":/image/max.png"));
            #endif
                emit MaxBtn_change();
            }
            //窗口还原
            else{
                if(need_close_picturepage){
                    emit close_picturePage();
                    //将该值还原为默认值
                    need_close_picturepage != need_close_picturepage;
                }
                #ifndef __V10__
                    m_pMaximizeButton->setIcon(QIcon::fromTheme("window-maximize-symbolic"));
                #else
                    m_pMaximizeButton->setIcon(QIcon(":/image/max.png"));
                #endif
                pWindow->showNormal();

            }
            m_maxButtonPressed == 0 ? m_maxButtonPressed = 1 : m_maxButtonPressed = 0;
        }
        else if (pButton == m_pCloseButton)
        {
            emit close();
//            pWindow->close();
        }
    }
}

//冗余代码 需优化
void TitleBar::dealSystemGsettingChange(const QString key){
    if(key == "styleName"){
        refreshThemeBySystemConf();
    }
}

void TitleBar::refreshThemeBySystemConf(){
    QString themeNow = m_pGsettingThemeData->get("styleName").toString();
    this->setGeometry(0,0,300,100);
    QPalette pal(this->palette());
    //深色模式下
    if (themeNow == "ukui-dark" || themeNow == "ukui-black"){
        //设置背景色黑色
        pal.setColor(QPalette::Background,Qt::black);
        icon->setStyleSheet("background-color:black");
    }
    else{
        //设置背景色白色
        pal.setColor(QPalette::Background,Qt::white);
        icon->setStyleSheet("background-color:white");
    }
    this->setAutoFillBackground(true);
    this->setPalette(pal);
    this->show();
}
